//
//  NSString+HBCategory.m
//  HBLibary
//
//  Created by Rambo on 2018/6/8.
//

#import "NSString+HBCategory.h"

@implementation NSString (HBCategory)

/**
 检查是否为数字
 
 @param string 文本
 @return 结果
 */
+ (BOOL)hb_validateNumber:(NSString*)string{
    
    if (string == nil || string.length <= 0) {
        return NO;
    }
    
    BOOL result = YES;
    
    NSCharacterSet *characterSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
    NSInteger idx = 0;
    while (idx < string.length) {
        NSRange range = [string rangeOfComposedCharacterSequenceAtIndex:idx];
        NSString *str = [string substringWithRange:range];
        NSRange textRange = [str rangeOfCharacterFromSet:characterSet];
        if (textRange.length == 0) {
            result = NO;
            break;
        }
        idx += range.length;
    }
    return result;
}

/**
 计算文本大小
 
 @param maxSize 最大size
 @param option option description
 @param font 字体大小
 @return 大小
 */
- (CGSize)hb_sizeWithMaxSize:(CGSize)maxSize options:(NSStringDrawingOptions)option font:(UIFont*)font{
    if (!font || CGSizeEqualToSize(maxSize, CGSizeZero)) return CGSizeZero;
    return [self boundingRectWithSize:maxSize options:option attributes:@{NSFontAttributeName : font} context:nil].size;
}

/**
 检查是否为空
 
 @param string 文本
 @return 结果
 */
+ (BOOL)hb_isEmpty:(NSString*)string{
    if ([string isKindOfClass:[NSNumber class]]) {
        string = [NSString stringWithFormat:@"%@",string];
    }
    if (string == nil || [string isKindOfClass:[NSNull class]] || ![string isKindOfClass:[NSString class]] || [string isEqualToString:@""] || string.length == 0 ) {
        return YES;
    }
    return NO;
}

/*
 *  设置行间距和字间距
 *
 *  @param string    字符串
 *  @param lineSpace 行间距
 *  @param kern      字间距
 *  @param font      字体大小
 *
 *  @return 富文本
 */
+ (NSAttributedString *)hb_getAttributedWithString:(NSString *)string WithLineSpace:(CGFloat)lineSpace kern:(CGFloat)kern font:(UIFont *)font{
    if ([self hb_isEmpty:string]) return nil;
    NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
    //调整行间距
    paragraphStyle.lineSpacing = lineSpace;
    NSDictionary *attriDict = @{NSParagraphStyleAttributeName:paragraphStyle,
                                            NSKernAttributeName:@(kern),
                                            NSFontAttributeName:font};
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:string attributes:attriDict];
    return attributedString;
}

/**
 字符串添加删除线
 
 @param string 字符串
 @param range range
 @return 删除线字符串
 */
+ (NSAttributedString *)hb_underlinWithString:(NSString*)string range:(NSRange)range{
    if ([self hb_isEmpty:string]) return nil;
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string
                                                                                         attributes:@{NSStrikethroughStyleAttributeName : @(NSUnderlineStyleNone)}];
    
    if (range.location < string.length && range.location+range.length <= string.length) {
        [attributedString setAttributes:@{NSStrikethroughStyleAttributeName : @(NSUnderlineStyleSingle),
                                          NSBaselineOffsetAttributeName : @0} range:range];
    }
    
    return attributedString.copy;
}


@end
